﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace IRCServer.Entities.Commands
{
    public class CommandFactory
    {
        public static IRCCommandBase GetCommandFromMessage(string message, ref SessionBase session)
        {
            IRCCommandBase command;

            try
            {
                command = new USERCommand(message);
                if (!(session is ClientSession))
                {
                    session = new ClientSession()
                    {
                        Socket = session.Socket
                    };
                }
                return command;
            }
            catch
            {
            }

            try
            {
                command = new NICKCommand(message);
                if (!(session is ClientSession))
                {
                    session = new ClientSession()
                    {
                        Socket = session.Socket
                    };
                }
                return command;
            }
            catch
            {
            }


            try
            {
                command = new SERVERCommand(message);
                session = new ServerSession()
                {
                    Socket = session.Socket,
                    ServerNodeID = ((SERVERCommand)command).NodeID
                };
                return command;
            }
            catch
            {
            }


            try
            {
                command = new SPRIVMSGCommand(message);
                return command;
            }
            catch
            {
            }

            if (session is ClientSession && ((ClientSession)session).ConnectionState == ConnectionState.NotRegistered)
                throw new InvalidOperationException();

            try
            {
                command = new PRIVMSGCommand(message);
                return command;
            }
            catch
            {
            }

            try
            {
                command = new QUITCommand(message);
                return command;
            }
            catch
            {
            }

            throw new InvalidOperationException();
        }
    }
}
